/**
 * 业务人员指定客户设置 - 编辑页面
 * @author miaoyl
 * @date 2018/01/16
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        element,
        table,
        vueData = {
            isAdd: false,
            form: '',
            rules: {
                GWBP001_NAME: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWBP002_NAME: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWBP003: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWBPACTI: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ]
            },
            selectOptions: {
                sys_y_n: app.datas_("sys_y_n"),
                gwbp003Options: app.datas_("crm_gwai501_gwax011")
            },
        },
        callObj,
        pageVue
    ;

    var Gwai303Edit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwai303_edit',
                _loadTplsFull: [
                    {dom: 'common_open_window', tpl: 'common/_open_window'}
                ]
            }, win);
        },

        editData: function () {
            app.ajax({
                xurl: 'crm/gwai303/edit',
                xdata: vueData.form,
                loading: true,
                okmsg: '业务人员指定客户信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        //同步更新缓存对应的值
                        callObj.update(result.xdata);
                        layer.close(callObj.layOpenIndex);
                    }
                }
            });
        },

        addData: function () {
            app.ajax({
                xurl: 'crm/gwai303/add',
                xdata: vueData.form,
                loading: true,
                okmsg: '业务人员指定客户信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('gwai303').reloadTableCurr();
                        layer.close(callObj.layOpenIndex);
                    }
                }
            });
        },

        _call: function (obj) {
            pageVue.$refs['form'].resetFields();
            callObj = obj;
            if (obj.is_add) {
                vueData.isAdd = true;
                // app.log(obj);
                app.extendUpLow({
                    target: vueData.form,
                    res: {
                        GWBPACTI: 'Y',
                        GWBP003: 'Y'
                    },
                    reset: true
                });
            } else {
                vueData.isAdd = false;
                app.extendUpLow({
                    target: vueData.form,
                    res: [callObj.data,{
                        oldGWBP001: callObj.data.GWBP001,
                        oldGWBP002: callObj.data.GWBP002,
                    }],
                    reset: true
                });
            }
        },
        _ready: function () {
            vueData.form = app.fields_(['crm_gwai303']);
            pageVue = new Vue({
                el: '#gwai303_edit',
                data: vueData,
                methods: {
                    cancel: function () {
                        layer.close(callObj.layOpenIndex);
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                if (vueData.isAdd) {
                                    self.addData();
                                } else {
                                    self.editData();
                                }
                            }
                        });
                    },
                    openYwy:function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method:'q_sysuser_id',
                                    columnList: [
                                        {label: '业务员编号', prop: 'ID', width: '150'},
                                        {label: '业务员名称', prop: 'NAME', width: '150'}
                                    ]
                                },
                                cb:function (data) {
                                    vueData.form.GWBP001_NAME = data.NAME;
                                    vueData.form.GWBP001 = data.ID;
                                }
                            },
                            index:0,
                            openOpts: {
                                title: '选择业务员',
                                area:['600px', '470px']
                            }
                        });
                    },
                    openKh:function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method:'q_gwak001',
                                    columnList: [
                                        {label: '客户编号', prop: 'GWAK001', width: '150'},
                                        {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                        {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                    ]
                                },
                                cb:function (data) {
                                    vueData.form.GWBP002_NAME = data.GWAKL003;
                                    vueData.form.GWBP002 = data.GWAK001;
                                }
                            },
                            index:0,
                            openOpts: {
                                title: '选择客户',
                                area:['600px', '470px']
                            }
                        });
                    }
                },
                created: function () {
                    self._setEleIsReady('gwai303_edit');
                }
            });

        },

        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

    return new Gwai303Edit();

});
